package com.hubujun.driverlicense.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.hubujun.driverlicense.DLAppMgr;
import com.hubujun.driverlicense.DLApplication;
import com.hubujun.driverlicense.util.Ln;
import com.hubujun.driverlicense.util.MobileUtils;

public class EntryPointActivity extends AppCompatActivity {

    private final String PageName = getClass().getSimpleName();

    public static final String KEY_COMPONENT_NAME = "component_name";
    public static final String KEY_SHOW_PROGRAM_ACTIVITY = "show_program_activity";
    public static final String CAN_FINISH = "can_finish";

    private static int mTaskId = 0;
    private boolean mFinishAfterResume = false;

    public static Intent getLauchIntent(Context context) {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.setClassName(context, EntryPointActivity.class.getName());
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        if (!(context instanceof Activity)) {
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        return intent;
    }

    private Intent intent;

    @Override
    protected void onNewIntent(Intent i) {
        Ln.d("EntryPointActivity onNewIntent");
        intent = i;
        super.onNewIntent(i);
        setIntent(i);
    }

    @Override
    public void onCreate(Bundle bundle) {
        Ln.d("EntryPointActivity onCreate taskid = %s", getTaskId());
        if (intent == null)
            intent = getIntent();
        Intent mainIntent = getIntent();
        String action = mainIntent.getAction();
        super.onCreate(bundle);
        if (mainIntent != null && mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
            if (!this.isTaskRoot()) {
                finish();
            }
            if (MobileUtils.canCreateShort())
                DLAppMgr.addShortCut();
        }
        if (mTaskId == getTaskId()) {
            mFinishAfterResume = true;
        } else {
            mFinishAfterResume = false;
            mTaskId = getTaskId();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        Ln.d("EntryPointActivity onResume");
        super.onResume();
        if (intent == null)
            intent = getIntent();

        if (intent.getBooleanExtra(DLAppMgr.ABSOLUTELY_EXIT, false)) {
            Ln.d("exit absolutely!!!");
            Intent i = (Intent) intent.getParcelableExtra("exit_and_view");
            if (i != null) {
                Ln.d("jump to exit:%s", i);
                startActivity(i);
            }
            finish();
            DLAppMgr.processExit();
            return;
        } else if (intent.getBooleanExtra(CAN_FINISH, false)) {
            Ln.d("exit obviously");
            DLApplication.mMainHandler.postDelayed(new Runnable() {

                @Override
                public void run() {
                    finish();
                }
            }, 100L);
        } else {
            ComponentName componentName = intent.getParcelableExtra(KEY_COMPONENT_NAME);
            if (componentName == null) {
                startActivity(MainActivity.intentFor(EntryPointActivity.this));
            } else {
                gotoIntentComponent();
            }
        }
        if (mFinishAfterResume) {
            finish();
        }
        intent = null;
    }

    @Override
    public void onPause() {
        super.onPause();
    }


    @Override
    public void onDestroy() {
        Ln.d("EntryPointActivity onDestroy");
        super.onDestroy();
    }

    private void gotoIntentComponent() {
        Intent intent = new Intent();
        intent.setComponent((ComponentName) getIntent().getParcelableExtra(KEY_COMPONENT_NAME));
        getIntent().removeExtra(KEY_COMPONENT_NAME);
        intent.putExtras(this.intent);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Ln.d("EntryPointActivity onActivityResult requestCode = %s, resultCode = %s", requestCode, resultCode);
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 10:
                if (resultCode == RESULT_OK) {
                    getIntent().putExtra(KEY_SHOW_PROGRAM_ACTIVITY, true);
                }
                break;
            default:
                break;
        }
    }
}
